/*
 * Copyright (C) 2010 Graham Allan
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package edu.strath.cis.grallan.findbugs.adaptiveranking.population;

import edu.strath.cis.grallan.findbugs.adaptiveranking.UserDesignationToPositivesMap;
import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.internalAnnotations.DottedClassName;

/**
 * @author Graham Allan (grallan@cis.strath.ac.uk)
 */
public class PackagePopulation extends AbstractPopulation {

	final String packageName;

	/**
	 * Constructs a PackagePopulation with a given package name. Alerts added to this population
	 * must originate from the same package.
	 * 
	 * Private constructor, use static factory to construct.
	 * 
	 * @see #populationOf(String)
	 */
	private PackagePopulation(@DottedClassName String packageName, UserDesignationToPositivesMap designationMapper) {
		super(designationMapper);
		this.packageName = packageName;

	}

	public static PackagePopulation populationOf(@DottedClassName String packageName) {
		return new PackagePopulation(packageName, new UserDesignationToPositivesMap());
	}

	@Override
	protected void checkAlert(BugInstance bugAlert) throws NullPointerException, PopulationException {
		super.checkAlert(bugAlert);

		String bugPackageName = bugAlert.getPrimaryClass().getPackageName();
		if (!packageName.equalsIgnoreCase(bugPackageName))
			throw new PopulationException("This population is for classes belonging to package " + packageName
					+ ". It is invalid to add bug alert for package " + bugPackageName);
	}

	public String populationDescriptor() {
		return packageName;
	}

}
